package com.scrum.Object;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.mysql.jdbc.Statement;
import com.scrum.Connection.DBConnection;

public class Comment {
	int id;
	int user_id;
	int post_id;
	String comment;
	
	public Comment(){
		
	}
	
	public Comment(int id, int user_id, int post_id, String comment) {
		super();
		this.id = id;
		this.user_id = user_id;
		this.post_id = post_id;
		this.comment = comment;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUser_id() {
		return user_id;
	}
	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}
	public int getPost_id() {
		return post_id;
	}
	public void setPost_id(int post_id) {
		this.post_id = post_id;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public static Comment addComment(int user_id,	int post_id, String comment) throws ClassNotFoundException, SQLException {
		Comment comment1 = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
					String sql = "insert into comment(user_id, post_id, comment) values (?,?,?)";
					con = DBConnection.connectDB();
					pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
					pstmt.setInt(1, user_id);
					pstmt.setInt(2, post_id);
					pstmt.setString(3, comment);
					pstmt.executeUpdate();
					rs = pstmt.getGeneratedKeys();
					
					if (rs != null && rs.next()) {
						comment1 = new Comment(rs.getInt(1), user_id, post_id, comment);
				}else{
					System.out.println("rs null");
				}
		
		} finally {
			try {
				rs.close();
				pstmt.close();
				con.close();
			} catch (Exception ex) {
			}
		}
		return comment1;
	}
	public static int getcountComments(int postid) throws ClassNotFoundException, SQLException{
	int countpost = 0;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        String sql = "SELECT COUNT(*) AS NumberOfComments FROM comment where post_id = ?";
        con = DBConnection.connectDB();
        pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, postid);
        rs = pstmt.executeQuery();
        if (rs.next()) {
        	countpost = rs.getInt("NumberOfComments");
        }
    }
    finally {
            rs.close();
            pstmt.close();
            con.close();
    }
    return countpost;
}
	public static Vector<Comment> getAllCommentByPostId(int post_id) throws SQLException, ClassNotFoundException{
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Vector<Comment> commentarray = new Vector<Comment>();
		try {
			String sql = "SELECT * FROM comment where post_id = "+post_id;
			con = DBConnection.connectDB();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				commentarray.add(new Comment(rs.getInt("id"), rs.getInt("user_id"), rs.getInt("post_id"), rs.getString("comment")));
			}
		}
		finally {
			rs.close();
			pstmt.close();
			con.close();
		}
		System.out.println(commentarray.size());
		return commentarray;
	}
	
}
